package cn.wubo.swagger.controller;

import cn.wubo.swagger.entity.Book;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/testSwagger")
//修饰整个类，描述Controller的作用
@Api(value = "测试", description = "测试用的API", tags = "TestApi")
public class TestSwaggerController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "Hello,Swagger!";
    }

    //描述一个方法，或者说一个接口
    @ApiOperation(value = "你好", notes = "根据url的name来指定某人，ui上name必输")
    //单个参数描述
    @ApiImplicitParam(name = "name", value = "输入一个名字", required = true, dataType = "String")
    @RequestMapping(value = "/helloSomeone", method = RequestMethod.GET)
    public String helloSomeone(@RequestParam(value = "name") String name) {
        return String.format("Hello,%s!", name);
    }

    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping(value = "/hide", method = RequestMethod.GET)
    public String hideMethgod() {
        return "看不到我";
    }

    @ApiOperation(value = "你好1", notes = "根据url的name来指定某人，ui上name不必输，方法参数默认为Swagger")
    @ApiImplicitParam(name = "name", value = "一个名字(默认为Swagger)", dataType = "String")
    @RequestMapping(value = "/helloSomeone1", method = RequestMethod.GET)
    public String helloSomeone1(@RequestParam(value = "name", required = false, defaultValue = "Swagger") String name) {
        return String.format("Hello,%s!", name);
    }

    @ApiOperation(value = "你好2", notes = "根据url的name来指定某人，ui上name不必输，ui默认值为-.-，方法参数默认为Swagger")
    @ApiImplicitParam(name = "name", value = "一个名字(默认为Swagger)", defaultValue = "-.-", dataType = "String")
    @RequestMapping(value = "/helloSomeone2", method = RequestMethod.GET)
    public String helloSomeone2(@RequestParam(value = "name", required = false, defaultValue = "Swagger") String name) {
        return String.format("Hello,%s!", name);
    }

    //用paramType来描述查询参数类型
    //path 	以地址的形式提交数据
    //query 	直接跟参数完成自动映射赋值
    //body 	以流的形式提交 仅支持POST
    //header 	参数在request headers 里边提交
    //form 	以form表单的形式提交
    //@RequestParam 用于通过Request.getParameter() 获取的可直接转换为简单类型的情况，即requet uri方式和requet body的简单参数方式
    //@PathVariable 用于requet uri方式的处理
    //@RequestHeader 用于requet header方式的处理
    //@RequestBody 用于requet body方式的处理
    @ApiOperation(value = "你好3", notes = "根据请求header里的name来指定某人，ui上name必输")
    @ApiImplicitParam(name = "name", value = "输入一个名字", required = true, dataType = "String", paramType = "header")
    @RequestMapping(value = "/helloSomeone3", method = RequestMethod.GET)
    public String helloSomeone3(@RequestHeader(value = "name") String name) {
        return String.format("Hello,%s!", name);
    }

    @ApiOperation(value = "多个参数", notes = "根据url传入多个参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "string1", value = "字符串1", required = true, dataType = "String"),
            @ApiImplicitParam(name = "string2", value = "字符串2", required = true, dataType = "String")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Success!!!"),
            @ApiResponse(code = 401, message = "not authorized!!!"),
            @ApiResponse(code = 403, message = "forbidden!!!"),
            @ApiResponse(code = 404, message = "not found!!!") })
    @RequestMapping(value = "/multiplyParam", method = RequestMethod.GET)
    public String multiplyParam(@RequestParam(value = "string1") String string1, @RequestParam(value = "string2") String string2) {
        return String.format("string1:%s string2:%s", string1, string2);
    }

    @ApiOperation(value = "响应描述", notes = "响应描述")
    @RequestMapping(value = "/testResponses", method = RequestMethod.GET)
    public ResponseEntity<List<Book>> testResponses() {
        List<Book> books = new ArrayList<Book>() {{
            add(new Book("111", "AAA"));
            add(new Book("222", "BBB"));
        }};
        return new ResponseEntity<>(books,HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
